package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.annotation.AutoFill;
import com.sky.dto.CommentPageDTO;
import com.sky.dto.UserCommentDTO;
import com.sky.enumeration.OperationType;
import com.sky.vo.CommentVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface CommentMapper {
    /**
     * 分页查询
     * @param commentPageDTO
     * @return
     */
    Page<CommentVO> list(CommentPageDTO commentPageDTO);

    /**
     * 新增评论
     * @param userCommentDTO
     */
    @AutoFill(value = OperationType.INSERT)
    @Insert("insert into comment (content, article_id,nickname,email, create_time, update_time)" +
            "value (#{content},#{articleId},#{nickname},#{email},#{createTime},#{updateTime})")
    void addComment(UserCommentDTO userCommentDTO);

    /**
     * 删除评论
     * @param id
     */
    @Delete("delete from comment where id = #{id}")
    void deleteComment(Long id);

    /**
     * 统计评论数量
     * @return
     */
    @Select("select count(*) from comment")
    Long countComment();

    /**
     * 根据id查询评论
     * @param id
     * @return
     */
    @Select("select * from comment where id = #{id}")
    CommentVO getById(Long id);

    /**
     * 根据文章id统计评论数量
     * @param articleId
     * @return
     */
    @Select("select count(*) from comment where article_id = #{articleId}")
    Long countByArticleId(Long articleId);

    /**
     * 根据文章id查询评论id列表
     * @param id
     * @return
     */
    @Select("select id from comment where article_id = #{id}")
    List<Long> getCommentIdsByArticleId(Long id);

    /**
     * 根据文章id删除评论
     * @param id
     */
    @Delete("delete from comment where article_id = #{id}")
    void deleteCommentByArticleId(Long id);
}
